/*
 * ItemView.java
 *
 * Created on __DATE__, __TIME__
 */

package pl.streamsoft.test.currency_view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;

import net.sourceforge.jdatepicker.JDateComponentFactory;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import pl.streamsoft.test.currency_buisness.CurrenciesService;
import pl.streamsoft.test.currency_buisness.dto.CurrencyDTO;
import pl.streamsoft.test.currency_buisness.exceptions.ServiceException;
import pl.streamsoft.test.currency_view.helpers.DateHelper;
import pl.streamsoft.test.currency_view.storage.Storage;

/**
 * 
 * @author __USER__
 */
public class ItemView extends javax.swing.JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1641995465205373739L;

	/** Creates new form ItemView */
	public ItemView(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		currencyNameLabel = new javax.swing.JLabel();
		currencyValueLabel = new javax.swing.JLabel();
		currencyDateLabel = new javax.swing.JLabel();
		saveButton = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();
		currencyNameTextField = new javax.swing.JTextField();
		currencyValueTextField = new javax.swing.JTextField();
		currencyDateTextField = (JDatePickerImpl) JDateComponentFactory
				.createJDatePicker();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		currencyNameLabel.setText("Waluta");
		currencyValueLabel.setText("Warto\u015b\u0107");
		currencyDateLabel.setText("Data");

		saveButton.setText("Zapisz");
		cancelButton.setText("Anuluj");

		currencyDateTextField.getModel().setSelected(true);

		saveButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent action) {
				try {
					CurrenciesService controller = new CurrenciesService();
					CurrencyDTO item = getItemDao();
					switch (mode) {
					case VIEW_MODE_EDITION:
						controller.update(item);
						Storage.getInstance().getCurrencyTableModel()
								.reloadFromServer();
						break;
					case VIEW_MODE_CREATING:
						controller.create(item);
						Storage.getInstance().getCurrencyTableModel()
								.reloadFromServer();
						Storage.getInstance().newCurrencyAddedSignal.execute();

						break;
					}
					close();
				} catch (ServiceException e) {
					e.printStackTrace();
				}
			}
		});
		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				close();

			}
		});
		currencyNameTextField
				.setHorizontalAlignment(javax.swing.JTextField.LEFT);

		currencyValueTextField
				.setHorizontalAlignment(javax.swing.JTextField.LEFT);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addComponent(
																		saveButton)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		cancelButton))
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						currencyNameLabel)
																				.addComponent(
																						currencyDateLabel)
																				.addComponent(
																						currencyValueLabel))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						currencyValueTextField,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						206,
																						Short.MAX_VALUE)
																				.addComponent(
																						currencyDateTextField,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						206,
																						Short.MAX_VALUE)
																				.addComponent(
																						currencyNameTextField,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						206,
																						Short.MAX_VALUE))))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(currencyNameLabel)
												.addComponent(
														currencyNameTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(currencyDateLabel)
												.addComponent(
														currencyDateTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														currencyValueLabel)
												.addComponent(
														currencyValueTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										21, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(saveButton)
												.addComponent(cancelButton))
								.addContainerGap()));

		pack();
	}// </editor-fold>

	// GEN-END:initComponents

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton saveButton;
	private javax.swing.JButton cancelButton;
	private javax.swing.JLabel currencyNameLabel;
	private javax.swing.JLabel currencyValueLabel;
	private javax.swing.JLabel currencyDateLabel;
	private javax.swing.JTextField currencyNameTextField;
	private javax.swing.JTextField currencyValueTextField;
	private JDatePickerImpl currencyDateTextField;
	// End of variables declaration//GEN-END:variables

	public static final int VIEW_MODE_VIEWING = 1;
	public static final int VIEW_MODE_CREATING = 2;
	public static final int VIEW_MODE_EDITION = 3;

	private int mode = VIEW_MODE_VIEWING;
	private CurrencyDTO dto;

	public void actionButtonsVisibility(boolean value) {
		saveButton.setVisible(value);
		cancelButton.setVisible(value);
	}

	private void setViewingMode() {
		actionButtonsVisibility(false);
		currencyNameTextField.setEditable(false);
		currencyValueTextField.setEditable(false);
		currencyDateTextField.setEnabled(false);
	}

	private void setCreatingMode() {
		actionButtonsVisibility(true);
		currencyNameTextField.setEditable(true);
		currencyValueTextField.setEditable(true);
		currencyDateTextField.setEnabled(true);
	}

	private void setEditingMode() {
		actionButtonsVisibility(true);
		currencyNameTextField.setEditable(false);
		currencyValueTextField.setEditable(true);
		currencyDateTextField.setEnabled(false);
	}

	public void changeMode(int mode) {
		this.mode = mode;
		switch (mode) {
		case VIEW_MODE_VIEWING:
			setViewingMode();
			break;
		case VIEW_MODE_CREATING:
			setCreatingMode();
			break;
		case VIEW_MODE_EDITION:
			setEditingMode();
			break;
		}
	}

	public Double getEditorValue() {
		return Double.parseDouble((currencyValueTextField.getText()));
	}

	public Date getEditorDate() {
		return DateHelper.getDate(currencyDateTextField);
	}

	public void setEditorDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		currencyDateTextField.getModel().setDate(calendar.get(Calendar.YEAR),
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
	}

	public String getEditorAcronym() {
		return currencyNameTextField.getText();
	}

	public CurrencyDTO getItemDao() {
		if (dto == null) {
			dto = new CurrencyDTO();
		}
		this.dto.acronym = getEditorAcronym();
		this.dto.value = getEditorValue();
		this.dto.date = getEditorDate();
		return this.dto;
	}

	public void setDTO(CurrencyDTO dto) {
		this.dto = dto;
		currencyNameTextField.setText(dto.acronym);
		currencyValueTextField.setText(dto.value.toString());
		currencyDateTextField.getModel().setSelected(true);
		setEditorDate(dto.date);
	}

	public void close() {
		this.dispose();
	}

}